using System;
using System.ComponentModel;
using System.Collections;
using EnvDTE;
using System.IO;

namespace OWA.Installer.CustomActions
{
    [RunInstaller(true)]
    public class OWAToolboxInstaller
        : System.Configuration.Install.Installer
    {
        public override void Install(IDictionary stateSaver)
        {
            DTE dte = null;
            Window window = null;
            ToolBox toolBox = null;
            ToolBoxTabs tabs = null;
            ToolBoxTab tab = null;
            try
            {
                string assemblyPath = GetOWAAssemblyPath();
                dte = new EnvDTE.DTE();
                window = dte.Windows.Item(Constants.vsWindowKindToolbox);
                toolBox = (ToolBox)window.Object;
                tabs = toolBox.ToolBoxTabs;
                tab = tabs.Add("Office Workflow Activities");
                tab.Activate();
                tab.ToolBoxItems.Add(
                    "Office Workflow Activities",
                    assemblyPath,
                    vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent);
            }
            catch (Exception)
            {
            }
            finally
            {
                if (tab != null)
                {
                    tab = null;
                }
                if (tabs != null)
                {
                    tabs = null;
                }
                if (toolBox != null)
                {
                    toolBox = null;
                }
                if (window != null)
                {
                    window = null;
                }
                if (dte != null)
                {
                    dte = null;
                }
            }
        }
        
        string GetOWAAssemblyPath()
        {
            string path = Context.Parameters["AssemblyPath"];
            if (String.IsNullOrEmpty(path))
            {
                throw new ApplicationException("OWA assembly path not found.");
            }
            return Path.Combine(path, "OWA.Activities.dll");
        }
    }
}
